﻿import mx.controls.ProgressBar;
import RealisticBooks.XMLLoader;
import RealisticBooks.ParseText;
import RealisticBooks.BookSection;
import RealisticBooks.BookProperties;

/**
 * BookLoader
 * This class load and parse the book structure, annotation and bookmark files
 *
 * @author: Veronica Liesaputra
 */
class RealisticBooks.BookLoader {
/**
 * Indicating whether the application is run on a web server
 * @type Boolean
 */
private var onServer:Boolean;
/**
 * The url for the book structure file
 * @type String
 */
private var doc_url:String;
/**
 * The url for the annotation and bookmark structure file
 * @type String
 */
private var anno_url:String;
/**
 * The progress bar shown when loading an xml file
 * @type ProgressBar
 */
private var progressBar:ProgressBar;
/**
 * The text field
 * @type TextField
 */
private var txtField:TextField;
/**
 * The designer toolbar
 * @type MovieClip
 */
private var dtoolbar:MovieClip;
/**
 * The book toolbar
 * @type MovieClip
 */
private var btoolbar:MovieClip;
/**
 * Initialize the BookLoader
 * @constructor
 * Parameters:
 * @param {String} app_url the application url
 * @param {String} doc_url the book structure url
 * @param {String} anno_url the annotation and bookmark url	
 * @param {ProgressBar} progressBar the progress bar
 * @param {MovieClip} dtoolbar the designer toolbar
 * @param {TextField} txtField the text field
 * @param {MovieClip} btoolbar the book toolbar
 */
public function BookLoader (app_url:String,doc_url:String,
							anno_url:String,progressBar:ProgressBar,
							dtoolbar:MovieClip, txtField:TextField,
							btoolbar:MovieClip) {
	this.doc_url = doc_url;
	this.anno_url = anno_url;
	this.progressBar = progressBar;
	this.dtoolbar = dtoolbar;
	this.txtField = txtField;
	this.btoolbar = btoolbar;
	this.isOnServer(app_url);
}
/**
 * Whether the application is run on a web server
 * Parameters:
 * @param {String} app_url the url of the application
 */
private function isOnServer (app_url:String):Void {
	this.onServer = true;
	if (app_url.indexOf("file") == 0) {
     	this.onServer = false;
  	}
}
/**
 * Display the error status when loading the XML file
 * on the progress bar
 * @param {Number} xmlStatus the xml error code
 */
private function errorStatusXML(xmlStatus:Number):Void {	
	this.progressBar.label = XMLLoader.errorMessageXML(xmlStatus);
	this.progressBar.indeterminate = false;
}
/**
 * Load and parse the book structure
 */
public function loadBook ():Void {
	var docLoader:XMLLoader = new XMLLoader(parseBookXML, parseBookFail, this);
	docLoader.loadXML(this.doc_url,this.onServer);
}
/**
 * Load and parse the anno structure
 */
public function loadAnno ():Void {
	this.progressBar.label = "Setting bookmarks and annotatons ...";
	var annoLoader:XMLLoader = new XMLLoader(parseAnnoXML, parseAnnoFail, this);
	annoLoader.loadXML(this.anno_url,this.onServer);
}
private function parseBookXML (myXML:XML, obj:BookLoader):Void {
	//var bp = new BookProperties();
	//bp.duplicateFrom(obj.btoolbar._parent);
	var pt:ParseText = new ParseText (new BookProperties(),obj.btoolbar);
	var st = getTimer();
	var str = pt.parseRoot(myXML, new BookSection("","",0), obj.txtField);
	var et = getTimer() - st; trace('parsing:' + et);
	//pt.testOutput();
	
	//obj.btoolbar._parent.loadXMLFromURL(obj.btoolbar._parent.doc_url);
	var doc_xml:XML = new XML(str);
	obj.btoolbar._parent.parseXML(doc_xml);
	obj.dtoolbar.initBtnCover();
}
private function parseBookFail (xmlStatus:Number, obj:BookLoader):Void {
	obj.errorStatusXML(xmlStatus);
	obj.dtoolbar.initBtnCover();
}
private function parseAnnoXML (myXML:XML, obj:BookLoader):Void { }
private function parseAnnoFail (xmlStatus:Number, obj:BookLoader):Void {
	if (xmlStatus == -1) {
		obj.finalizeParse();
	} else {
		obj.errorStatusXML(xmlStatus);
	}
}
private function finalizeParse () { }
}
 